package com.wk.springbootlearn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wk.springbootlearn.common.utils.CompareList;
import com.wk.springbootlearn.mapper.GroupMapper;
import com.wk.springbootlearn.mapper.GroupTeamMapper;
import com.wk.springbootlearn.mapper.TeamMapper;
import com.wk.springbootlearn.pojo.entity.GroupTeam;
import com.wk.springbootlearn.pojo.entity.MyGroup;
import com.wk.springbootlearn.pojo.entity.Team;
import com.wk.springbootlearn.pojo.form.GetGroupForm;
import com.wk.springbootlearn.pojo.form.GroupForm;
import com.wk.springbootlearn.pojo.model.GroupListItemModel;
import com.wk.springbootlearn.pojo.model.GroupListModel;
import com.wk.springbootlearn.service.GroupService;
import lombok.Setter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class GroupServiceImpl implements GroupService {

    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private TeamMapper teamMapper;

    @Autowired
    private GroupTeamMapper groupTeamMapper;

    @Override
    @Transactional
    public void createGroup(GroupForm groupForm, Long userId) {
        //判断团队id是否有效
        if (groupForm.getTeamIdList() != null) {
            for (Long teamId : groupForm.getTeamIdList()) {
                Team team = teamMapper.selectById(teamId);
                if (team == null) throw new RuntimeException("无效的团队id：" + teamId);
            }
        }

        // 创建小组
        MyGroup group = new MyGroup();
        BeanUtils.copyProperties(groupForm, group);
        group.setCreator(userId);
        group.setUpdator(userId);
        int insert = groupMapper.insert(group);
        // 创建小组和团队的关系
        if (insert > 0 && groupForm.getTeamIdList() != null && groupForm.getTeamIdList().length > 0) {
            GroupTeam groupTeam = new GroupTeam();
            groupTeam.setGroupId(group.getId());
            groupTeam.setCreator(userId);
            groupTeam.setUpdator(userId);
            for (Long teamId : groupForm.getTeamIdList()) {
                groupTeam.setTeamId(teamId);
                groupTeamMapper.insert(groupTeam);
            }

        }

    }

    @Override
    @Transactional
    public void updateGroup(Long id, GroupForm groupForm, Long userId) {
        // 判断小组id是否有效
        MyGroup myGroup = groupMapper.selectById(id);
        if (myGroup == null) throw new RuntimeException("无效的小组id");
        // 判断团队id是否有效
        QueryWrapper<GroupTeam> groupTeamWrapper = new QueryWrapper<GroupTeam>().eq("group_id", id);
        if (groupForm.getTeamIdList() == null || groupForm.getTeamIdList().length == 0) {
            // 删除小组团队关联记录
            groupTeamMapper.delete(groupTeamWrapper);
        }else{
            // 判断团队id是否有效
            for (Long sourceTeamId : groupForm.getTeamIdList()) {
                Team team = teamMapper.selectById(sourceTeamId);
                if (team == null) throw new RuntimeException("无效的team_id"+sourceTeamId);
            }
            // 获取需要新增或删除的小组团队关联记录
            List<GroupTeam> groupTeams = groupTeamMapper.selectList(groupTeamWrapper);
            List<Long> sourceTeamList = groupTeams.stream().map(GroupTeam::getTeamId).collect(Collectors.toList());
            // 需要增加的teamId
            List<Long> addaListThanbList = CompareList.getAddaListThanbList(Arrays.asList(groupForm.getTeamIdList()), sourceTeamList);
            // 需要删除的teamId
            List<Long> reduceaListThanbList = CompareList.getReduceaListThanbList(Arrays.asList(groupForm.getTeamIdList()), sourceTeamList);
            // 新增小组团队联系
            addaListThanbList.forEach(ele -> {
                GroupTeam groupTeam = new GroupTeam();
                groupTeam.setGroupId(id);
                groupTeam.setTeamId(ele);
                groupTeam.setCreator(userId);
                groupTeam.setUpdator(userId);
                groupTeamMapper.insert(groupTeam);
            });
            // 删除小组团队联系
            reduceaListThanbList.forEach(ele -> {
                QueryWrapper<GroupTeam> delTeamIdWrapper = new QueryWrapper<GroupTeam>().eq("team_id", ele);
                groupTeamMapper.delete(delTeamIdWrapper);
            });
        }
        // 更新小组信息
        if (!groupForm.getGroupName().equals(myGroup.getGroupName())) {
            MyGroup myGroup1 = new MyGroup();
            myGroup1.setId(id);
            myGroup1.setGroupName(groupForm.getGroupName());
            myGroup1.setUpdator(userId);
            groupMapper.updateById(myGroup1);
        }
    }

    @Override
    public IPage<GroupListItemModel> findGroup(GetGroupForm getGroupForm) {
        if (getGroupForm.getTeamId() != null) {
            Team team = teamMapper.selectById(getGroupForm.getTeamId());
            if (team == null) throw new RuntimeException("无效的团队id"+getGroupForm.getTeamId());
        }
        Page<GroupTeam> teamPage = new Page<>(getGroupForm.getPage(), getGroupForm.getPageSize());
        return groupMapper.findGroupList(teamPage, getGroupForm);
    }

    @Override
    @Transactional
    public void delGroup(Long id) {
        // 查看小组是否存在
        MyGroup myGroup = groupMapper.selectById(id);
        if (myGroup == null) throw new RuntimeException("无效的小组id" + id);
        // 删除该小组的团队小组关系记录
        QueryWrapper<GroupTeam> queryWrapper = new QueryWrapper<GroupTeam>().eq("group_id", id);
        groupTeamMapper.delete(queryWrapper);
        // 删除该小组
        groupMapper.deleteById(id);
    }
}
